﻿@using EquipmentManager.Entities
@model List<EquipmentBase>

@{
    Layout = "";
    ViewBag.Title = "ExportAllEquipments";
}

@{
    string fileDateTime = String.Join("", new object[] {DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second});
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/ms-excel";
    Response.AddHeader("Content-disposition", String.Join("", new object[] { "attachment;filename=export_", fileDateTime, ".csv" }));
    Response.ContentEncoding = System.Text.Encoding.UTF8;
    Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());

    string sep = "";
    Response.Write(sep + "Инвентарный номер;Серийный номер;Название;Описание;Заметки;Рабочее место;Год;Состояние;Дата создания;Отделение;Район;Дирекция");
    Response.Write("\n");
    
    foreach (EquipmentBase equipment in Model)
    {
        sep = "";

        Response.Write(sep + equipment.Number);
        sep = ";";
        Response.Write(sep + equipment.SerialNumber);
        Response.Write(sep + equipment.Name);
        Response.Write(sep + equipment.Description);
        if(equipment.Note != null)
        {
            Response.Write(sep + equipment.Note.Replace("\r\n", string.Empty));
        }
        else
        {
            Response.Write(sep + "");
        }
        Response.Write(sep + equipment.WorkPlace);
        Response.Write(sep + equipment.Year);
        Response.Write(sep + equipment.State);
        Response.Write(sep + equipment.CreatedDate);
        Response.Write(sep + equipment.PostOffice.Name);
        Response.Write(sep + equipment.PostOffice.Region.Name);
        Response.Write(sep + equipment.PostOffice.Region.Directorate.Name);
        
        Response.Write("\n");
    }

    Response.End();
}

